import { t } from 'i18next'
import React, { FC, useEffect, useState } from 'react'
import { ListFormItem, RightWrap } from '../../components'
import Alert from '@/components/Alert'
import { Switch, Button } from '@kedacom/react-next'
import { fetchRecord, fetchSign, fetchSubtitle } from '@/api/manager/ai'
import useLock from '@/hooks/useLock'
import { useAIStore } from './store'
import shallow from 'zustand/shallow'
import { useInterval } from 'ahooks'
import { fetchGetIntelligenceStatus, fetchPowerOnAll } from '@/api/manager/ai'
import { useMccStore } from '../../store'
import { getCurrentTabType } from '../main/store'

const Ai: FC = () => {
	const meetingMoid = useMccStore((state) => state.mccInfo.meetingMoid)
	const [interval, setInterval] = useState<number | undefined>(2000)
	const [powerOnAlInterval, setPowerOnAlInterval] = useState<number | undefined>(undefined)
	const { kisOperation, update, kisList } = useAIStore(
		(state) => ({
			kisOperation: state.kisOperation,
			update: state.update,
			kisList: state.kisList,
		}),
		shallow
	)
	const clear = useInterval(() => {
		fetchGetIntelligenceStatus(meetingMoid).then((res) => {
			if (res) {
				update({
					kisOperation: {
						record: res.record_status === 1,
						sign: res.sign_status === 1,
						subtitle: res.subtitle_status === 1,
					},
				})
			}
		})
	}, interval)
	const powerOnAllTimer = useInterval(() => {
		const currentTabType = getCurrentTabType()
		for (const item of kisList) {
			item.showCalling[currentTabType] = false
		}
		update({ kisList: [...kisList] })
		setPowerOnAlInterval(undefined)
	}, powerOnAlInterval)
	useEffect(() => {
		const unsub1 = useAIStore.subscribe(
			(x) => x.isOpen,
			(isOpen) => {
				if (isOpen) {
					fetchGetIntelligenceStatus(meetingMoid).then((res) => {
						if (res) {
							update({
								kisOperation: {
									record: res.record_status === 1,
									sign: res.sign_status === 1,
									subtitle: res.subtitle_status === 1,
								},
							})
						}
					})
					setInterval(interval)
				} else {
					setInterval(undefined)
				}
			}
		)
		return () => {
			unsub1()
			update({ isOpen: false })
		}
	}, [])
	const handleBack = () => {
		useMccStore.getState().setCurPage('mcc_main')
	}
	const handleRecord = useLock(async () => {
		const operation = kisOperation.record ? 0 : 1
		const res = await fetchRecord(operation, meetingMoid)
		if (res && res.success) {
		} else {
			Alert(res.description || t('操作失败'))
		}
	}, 3000)
	const handleSign = useLock(async () => {
		const operation = kisOperation.sign ? 0 : 1
		const res = await fetchSign(operation, meetingMoid)
		if (res && res.success) {
		} else {
			Alert(res.description || t('操作失败'))
		}
	}, 3000)
	const handleSubtitle = useLock(async () => {
		const operation = kisOperation.subtitle ? 0 : 1
		const res = await fetchSubtitle(operation, meetingMoid)
		if (res && res.success) {
		} else {
			Alert(res.description || t('操作失败'))
		}
	}, 3000)
	const handleOpenAll = useLock(async () => {
		const currentTabType = getCurrentTabType()
		for (const item of kisList) {
			item.operation = false
			item.showCalling[currentTabType] = true
		}
		update({ kisList: [...kisList] })
		setPowerOnAlInterval(2000)
		const list = await fetchPowerOnAll(meetingMoid)
		if (list) {
			let _map: { [key: string]: boolean } = {}
			for (const item of list) {
				_map[item.mt_key] = item.operation === 1
			}
			for (const item of kisList) {
				if (_map) {
					item.operation = _map[item.mtKey] ?? item.operation
				}
			}
			update({ kisList: [...kisList] })
		}
	}, 3000)
	return (
		<>
			<RightWrap title="AI" onBack={handleBack}>
				<ListFormItem label={t('会议纪要')} height={40}>
					<Switch checked={kisOperation.record} onChange={handleRecord} />
				</ListFormItem>
				<ListFormItem label={t('同声字幕')} height={40}>
					<Switch checked={kisOperation.subtitle} onChange={handleSubtitle} />
				</ListFormItem>
				<ListFormItem label={t('会议签到')} height={40}>
					<Switch checked={kisOperation.sign} onChange={handleSign} />
				</ListFormItem>
				<ListFormItem label={t('远程开机')} height={40}>
					<Button onClick={handleOpenAll}>{t('全部开机')}</Button>
				</ListFormItem>
			</RightWrap>
		</>
	)
}
export default Ai
